/**
 * 
 */
package nc.vo.am.common.util;

import nc.vo.bd.access.AccessorManager;
import nc.vo.bd.access.IBDAccessor;
import nc.vo.bd.access.BddataVO;
import nc.vo.pub.SuperVO;

/**
 * @author zhaoss1
 *
 */
public class FromPKToNameUtils {
	
	public FromPKToNameUtils(){
		
	}
	
	private static IBDAccessor bdAccessor = null;
	private static BddataVO bdDataVO = null;
	private static String name = null;
	
	public static String changePK2Name(String pk_bdinfo, String pk_corp, String pk){
		
		bdAccessor = AccessorManager.getAccessor(pk_bdinfo, pk_corp);
		if(bdAccessor != null){
			bdDataVO = bdAccessor.getDocByPk(pk);
			if(bdDataVO != null)
				name = bdDataVO.getName();
		}
		
		return name;
	}
	
	public static void changePKToName(SuperVO[] superVOs, String pk_bdinfo, String pk_alias, String field_alias){
		
		if (superVOs == null)
			return;
	   
	    if(superVOs.length > 0){ 
		    String pk_corp = null;
		    Object primary_key = null;
		    
	    	for(int i = 0; i < superVOs.length; i++){	
	    	    primary_key = superVOs[i].getAttributeValue(pk_alias);
			    pk_corp = superVOs[i].getAttributeValue(CommonKeyUtils.pk_corp).toString();
			    
			    superVOs[i].setAttributeValue(field_alias, null);
			    
			    if(pk_bdinfo == null || pk_corp == null || primary_key == null || primary_key.toString().trim().length() <= 0)
			    	continue;
	    		bdAccessor = AccessorManager.getAccessor(pk_bdinfo, pk_corp);
	    		if(bdAccessor == null)
	    			continue;
	    		bdDataVO = bdAccessor.getDocByPk(primary_key.toString().trim());
	    		if(bdDataVO == null)
	    			continue;
	    		name = bdDataVO.getName();
	    		superVOs[i].setAttributeValue(field_alias, name);
	    	}
	    }
	    
	}

}
